#include "_LDIF.h"
#include<iostream>

int _LDIF::count;

///salut

int _LDIF::dimensiune()
{
                int dim=0;
                for(int i=0;i<atribute.size();i++)
                        dim+=strlen(atribute[i].daAtribut())+strlen(atribute[i].daNume())+1;
                return dim;
}


char* _LDIF::Da_LDIF()
 {

	 if(atribute.size())
	 {
                char *format=new char[dimensiune()+1];
                if(format==0)
                        throw new exception();
                strcpy(format,"");
                strcat(format,atribute[0].daNume());
                strcat(format,": ");
                strcat(format,atribute[0].daAtribut());
                strcat(format,";");
                for(int i=1;i<atribute.size()-1;i++)
                {
                        strcat(format,atribute[i].daNume());
                        strcat(format,": ");
                        strcat(format,atribute[i].daAtribut());
                        strcat(format,";");
                }
				if(atribute.size()>1)
				{
					strcat(format,atribute[atribute.size()-1].daNume());
					strcat(format,": ");
					strcat(format,atribute[atribute.size()-1].daAtribut());
				}
				else
				{
					format[strlen(format)-1] = ' ';
				}

                char name[20],c[10],*p,ter[]=";";

                strcpy(name,"ldif_file");
                _itoa(count,c,10);
                strcat(name,c);
                strcat(name,".ldif");

                file_name = new char[strlen(name)+1];
                strcpy(file_name,name);

                g=fopen(file_name,"w");

                p=strtok(format,ter);
                fprintf(g,"%s",p);
                p=strtok(NULL,ter);
                while(p)
                {

                        if(!strstr(p,"dn"))
                                fprintf(g,"\n%s",p);
                        else
                                fprintf(g,"\n\n%s",p);

                        p=strtok(NULL,ter);

                }
	
		fclose(g);
        return file_name;
	 }
	 else
	 {
		 printf("\nVectorul de atribute este NULL!...");
		 file_name = new char[1];
		 strcpy(file_name,"");
		 return file_name;
	 }
 }

